package com.mail.manager.modular.controller.portal;

import com.mail.manager.common.annotion.Permission;
import com.mail.manager.common.annotion.log.BussinessLog;
import com.mail.manager.common.constant.Dict;
import com.mail.manager.common.constant.factory.ConstantFactory;
import com.mail.manager.common.controller.BaseController;
import com.mail.manager.common.exception.BizExceptionEnum;
import com.mail.manager.common.exception.BussinessException;
import com.mail.manager.common.node.ZTreeNode;
import com.mail.manager.core.log.LogObjectHolder;
import com.mail.manager.core.util.ToolUtil;
import com.mail.manager.modular.model.ProductCategory;
import com.mail.manager.modular.service.portal.IProductCategoryService;
import com.mail.manager.modular.warpper.DeptWarpper;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * 创建人： Shi Heng
 * 创建时间：2018-04-03 18:55
 * QQ：374696376
 * Description：
 */
@Controller
@RequestMapping("/portal/product/category")
public class ProductCategoryController extends BaseController {

    @Resource
    IProductCategoryService categoryService;
    private static String PREFIX = "/portal/product/category/";

    /**
     * 跳转到产品分类管理首页
     */
    @RequestMapping("")
    public String index() {
        return PREFIX + "category.html";
    }

    /**
     * 跳转到添加产品分类
     */
    @RequestMapping("/category_add")
    public String categoryAdd() {
        return PREFIX + "category_add.html";
    }

    /**
     * 跳转到修改产品分类
     */
    @Permission
    @RequestMapping("/category_update/{categoryId}")
    public String categoryUpdate(@PathVariable Integer categoryId, Model model) {
        ProductCategory category = categoryService.selectByPrimaryKey(categoryId);
        model.addAttribute(category);
        model.addAttribute("pName", ConstantFactory.me().getDeptName(category.getPid()));
        return PREFIX + "category_edit.html";
    }

    /**
     * 获取产品分类的tree列表
     */
    @RequestMapping(value = "/tree")
    @ResponseBody
    public List<ZTreeNode> tree() {
        List<ZTreeNode> tree = categoryService.tree();
        tree.add(ZTreeNode.createParent());
        return tree;
    }

    /**
     * 新增产品分类
     */
    @BussinessLog(value = "添加产品分类", key = "simplename", dict = Dict.DeptDict)
    @RequestMapping(value = "/add")
    @Permission
    @ResponseBody
    public Object add(ProductCategory category) {
        if (ToolUtil.isOneEmpty(category, category.getSimple_name())) {
            throw new BussinessException(BizExceptionEnum.REQUEST_NULL);
        }
        //完善pids,根据pid拿到pid的pids
        categorySetPids(category);
        return categoryService.insert(category);
    }

    /**
     * 获取所有产品分类列表
     */
    @RequestMapping(value = "/list")
    @Permission
    @ResponseBody
    public Object list(String condition) {
        List<Map<String, Object>> list = categoryService.list(condition);
        return list;
    }

    /**
     * 产品分类详情
     */
    @RequestMapping(value = "/detail/{categoryId}")
    @Permission
    @ResponseBody
    public Object detail(@PathVariable("categoryId") Integer categoryId) {
        return categoryService.selectByPrimaryKey(categoryId);
    }

    /**
     * 修改产品分类
     */
    @BussinessLog(value = "修改产品分类", key = "simplename", dict = Dict.DeptDict)
    @RequestMapping(value = "/update")
    @Permission
    @ResponseBody
    public Object update(ProductCategory category) {
        if (ToolUtil.isEmpty(category) || category.getId() == null) {
            throw new BussinessException(BizExceptionEnum.REQUEST_NULL);
        }
        categorySetPids(category);
        categoryService.updateObjtById(category);
        return super.SUCCESS_TIP;
    }

    /**
     * 删除产品分类
     */
    @RequestMapping(value = "/delete")
    @ResponseBody
    public Object delete(@RequestParam Integer id) {

        //缓存被删除的产品分类名称
        LogObjectHolder.me().set(ConstantFactory.me().getDeptName(id));

        categoryService.delteObject(id);

        return SUCCESS_TIP;
    }

    private void categorySetPids(ProductCategory category) {
        if (ToolUtil.isEmpty(category.getPid()) || category.getPid().equals(0)) {
            category.setPid(0);
            category.setPids("[0],");
        } else {
            int pid = category.getPid();
            ProductCategory temp = categoryService.selectByPrimaryKey(pid);
            String pids = temp.getPids();
            category.setPid(pid);
            category.setPids(pids + "[" + pid + "],");
        }
    }
}
